﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Blis.Client;
using Blis.Common;
using Blis.Common.Utils;
using HarmonyLib;
using UnityEngine;

namespace BlackSurvivalCheat
{
    static class PickUpRubish
    {
        static bool maintoggle;
        static List<PatchHandler> handles;
        static int target;
        static PickUpRubish()
        {
            maintoggle = true;
            target = 0;
            handles = new List<PatchHandler>();
            handles.Add(new PatchHandler { Prefix = AccessTools.Method(typeof(PickUpRubish), "RpcOpenItemBox_Action_Prefix"), TargetType = typeof(RpcOpenItemBox), TargetMethonName = "Action" });
        }

        public static void Load(Harmony harmony)
        {
            foreach (PatchHandler handler in handles)
            {
                handler.Patch(harmony);
            }
        }

        public static void UnLoad(Harmony harmony)
        {
            foreach (PatchHandler handler in handles)
            {
                handler.Unpatch(harmony);
            }
        }

        static bool RpcOpenItemBox_Action_Prefix(RpcOpenItemBox __instance, ClientService clientService)
        {
            if (!maintoggle)
                return true;
            GameClient gameClient = MonoBehaviourInstance<GameClient>.inst;
            foreach (Item item in __instance.items)
            {
                    gameClient.Request(new ReqTakeItem
                    {
                        targetId = __instance.targetId,
                        itemId = item.id
                    }, NetChannel.ReliableOrdered);
                    gameClient.Request(new ReqDropItemFromInventory
                    {
                        itemId = item.id,
                        madeType = item.madeType,
                        position = new Vector3 { x = 999999999, y = 999999999, z = 999999999 }
                    }, NetChannel.ReliableOrdered);
                    //UnityEngine.Debug.Log(item.ItemData.code);
            }
            return false;
        }
    }
}
